import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/comment/comment_model.dart';
import 'package:xvideos/hj_model/comment/reply_model.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/comment/comment_mixin.dart';
import 'package:xvideos/hj_page/community_business/post_detail_page/post_detail_page.dart';
import 'package:xvideos/hj_utils/api_service/vid_service.dart';

import '../../../hj_utils/history_record_util.dart';

class PostDetailController extends GetxController with CommentMixin {
  final scrollCtr = ScrollController();

  PostDetailPageArgument? postDetailPageArgument;
  VideoModel? model;
  bool isLoading = false;

  @override
  void onInit() {
    super.onInit();
    postDetailPageArgument = Get.arguments;
    id = postDetailPageArgument?.id;
    objType = 'video';
  }

  @override
  void onReady() {
    super.onReady();
    initData();
  }

  void initData() {
    isLoading = true;
    update();
    Future.wait([fetchPageData(), fetchComments(postDetailPageArgument?.id ?? '')]).then((value) {
      isLoading = false;
      update();
    });
  }

  Future fetchPageData() async {
    final res = await VidService.getVideoDetail(postDetailPageArgument?.id ?? '');
    if (res != null) {
      model = res;
      HistoryRecordUtil.insertHistoryModel<VideoModel>(model!, HistoryRecordType.post);
    }
    update();
  }

  // 上下篇切换
  void nextOrPrePost(String? exchangeId) {
    postDetailPageArgument?.id = exchangeId;
    id = exchangeId;
    page = 1;
    comments.clear();
    initData();
  }

  onCommentOrReplyAction({CommentModel? comment, ReplyModel? reply, bool? isSend}) async {
    readyReplyComment(comment, replyModel: reply, isSend: isSend);
  }
}
